package com.chwn.harulib.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Myshikin on 2017/3/24.
 * desc:日期转换
 */
public class TimeUtils {

    public static String dateToString(Date time) {
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat("yyyy-MM-dd");
        String ctime = formatter.format(time);
        return ctime;
    }

    public static String getAge(long time) {
        long l = System.currentTimeMillis() / 1000 - time;
        long year = l / (365 * 24 * 60 * 60);
        long month = (l % (365 * 24 * 60 * 60)) / (30 * 24 * 60 * 60);
        String age = "";
        if (year == 0 && month != 0) {
            age = month + "个月";
        } else if (year == 0 && month == 0) {
            age = "未满月";
        } else if (year != 0 && month == 0) {
            age = year + "周岁";
        } else {
            age = year + "岁" + month + "个月";
        }
        return age;
    }

    public static String dateToString(long time) {
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat("yyyy-MM-dd");
        String ctime = formatter.format(new Date(time));
        return ctime;
    }

    public static long stringTodate(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return simpleDateFormat.parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public static boolean isOneDay(long time_1, long time_2) {
        String string_1 = dateToString(time_1);
        String string_2 = dateToString(time_2);
        return string_1.equals(string_2);
    }

    public static boolean isOneDay(String time_1, String time_2) {
        if (time_1 == null || time_2 == null) {
            return false;
        }
        return time_1.equals(time_2);
    }

    /**
     * @return 1 time_1 > time_2
     * 0 time_1 = time_2
     * -1 time_1 < time_2
     */
    public static int isOldTime(String time_1, String time_2) {
        long time1 = stringTodate(time_1);
        long time2 = stringTodate(time_2);
        if (time1 > time2) {
            return -1;
        } else if (time1 == time2) {
            return 0;
        } else {
            return 1;
        }
    }

    public static String getUpdateTime(String storyCreateTime) {
        long update ;
        if (storyCreateTime.length() == 13) {
            update = (System.currentTimeMillis()  - Long.parseLong(storyCreateTime)) / 1000;
        } else {
            update = System.currentTimeMillis() / 1000 - Integer.parseInt(storyCreateTime);
        }
        if (update > (24 * 60 * 60)) {
            if (storyCreateTime.length() == 13) {
                return dateToString( Long.parseLong(storyCreateTime) );
            } else {
                return dateToString(Integer.parseInt(storyCreateTime) * 1000L);
            }

        } else {
            long hour = update / (60 * 60);

            if (hour == 0) {
                long min = (update % (60 * 60)) / 60;
                return min == 0 ? "刚刚更新" : min + "分钟前更新";
            } else {
                return hour + "小时前更新";
            }

        }

    }
}
